package com.xy.Servlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.entity.QBookFee;
import com.xy.model.BJi;
import com.xy.model.BookFee;
import com.xy.model.Category;
import com.xy.model.Level;
import com.xy.model.Major;
import com.xy.model.Student;
import com.xy.service.BookFeeService;
import com.xy.service.impl.BookFeeServiceImpl;
import com.xy.service.impl.CategoryServiceImpl;
import com.xy.service.impl.ClassServiceImpl;
import com.xy.service.impl.LevelServiceImpl;
import com.xy.service.impl.MajorServiceImpl;
import com.xy.service.impl.StudentServiceImpl;

/**
 * Servlet implementation class BookFeeServlet
 */
@WebServlet("/bookFeeServlet")
public class BookFeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookFeeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		BookFeeService service = new BookFeeServiceImpl();

		if ("goquery".equals(request.getParameter("status"))) {
			List<Object> clist = new CategoryServiceImpl().list(Category.class);
			List<Object> mlist = new MajorServiceImpl().list(Major.class);
			List<Object> llist = new LevelServiceImpl().list(Level.class);
			List<Object> blist = new ClassServiceImpl().list(BJi.class);
			request.setAttribute("clist", clist);
			request.setAttribute("mlist", mlist);
			request.setAttribute("llist", llist);
			request.setAttribute("blist", blist);
			request.setAttribute("pre", request.getParameter("pre"));
			if("pay".equals(request.getParameter("pre"))) {
				request.setAttribute("text", "交纳");
			}else	if("query".equals(request.getParameter("pre"))) {
				request.setAttribute("text", "查询");
			}else {
				request.setAttribute("text", "统计");
			}
			request.getRequestDispatcher("/pages/student/student005/pay_fee_query.jsp").forward(request, response);
		} else if ("query".equals(request.getParameter("status"))) {
			String pre = request.getParameter("pre");
			request.setAttribute("pre", pre);
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
			String list = null;
			String num = null;
			String bj = null;
			BJi bji = null;

			PageResults pageResults = null;

			if (request.getParameter("radio") != null) {
				request.getSession().setAttribute("radio", request.getParameter("radio"));
			}

			if ("n".equals(request.getSession().getAttribute("radio"))) {
				if (request.getParameter("list") == null) {
					num = request.getParameter("num") == null ? "%" : request.getParameter("num");
					list = num;
				} else {
					list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
					num = list;
				}
				pageResults = service.query(num, currentPage);
				try {
					bji = ((QBookFee) (pageResults.getResults().get(0))).getStudent().getbJi();
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e.getMessage());
				}
				pageResults.setPageSize(5);
				request.setAttribute("bj", bji);
				request.setAttribute("pageResults", pageResults);
				request.setAttribute("status", "query");
				request.getSession().setAttribute("radio", "n");
				request.setAttribute("list", list);

			} else {
				if (request.getParameter("list") == null) {
					bj = request.getParameter("bj") == null ? "%" : request.getParameter("bj");
					list = bj;
				} else {
					bj = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
					list = bj;
				}
				bji = (BJi) new ClassServiceImpl().get(Integer.valueOf(bj));
				pageResults = service.queryByC(bj, currentPage);
				pageResults.setPageSize(5);
				request.setAttribute("bj", bji);
				request.setAttribute("pageResults", pageResults);
				request.setAttribute("status", "query");
				request.getSession().setAttribute("radio", "c");
				request.setAttribute("list", list);
			}

			if ("pay".equals(pre)) {
				request.getRequestDispatcher("/pages/student/student005/pay_fee_query_result.jsp").forward(request,
						response);
			} else if ("query".equals(pre)) {
				request.getRequestDispatcher("/pages/student/student005/fee_query.jsp").forward(request,
						response);
			} else if ("count".equals(pre)) {
//				断点
//				
//				
//				
//				
//				
//				
				request.getRequestDispatcher("/pages/student/student005/fee_statistic_query_result.jsp").forward(request,
						response);
			}
		} else if ("gopay".equals(request.getParameter("status"))) {
			String [] checkboxes = request.getParameterValues("checkbox");
			String s = null;
			for(int i=0;i<checkboxes.length;i++) {
				if(s!=null)
					s = s + checkboxes[i] + ",";
				else
					s = checkboxes[i] + ",";
			}
			request.setAttribute("checkboxes", s);
			request.getRequestDispatcher(
					"/pages/student/student005/pay_fee.jsp").forward(request,
					response);
		} else if ("pay".equals(request.getParameter("status"))) {
			String checkboxe= request.getParameter("checkboxes");
			String[] s = checkboxe.split(",");
			String academicYear = request.getParameter("academicYear");
			String semester = request.getParameter("semester");
			String payfee = request.getParameter("payfee");
			for(int i=0;i<s.length;i++) {
				BookFee bf = new BookFee();
				bf.setAcademicYear(academicYear);
				bf.setSemester(semester);
				Student stu = (Student) new StudentServiceImpl().get(Integer.valueOf(s[i]));
				bf.setStudent(stu);
				bf.setbJi(stu.getbJi());
				bf.setPayFee(Integer.valueOf(payfee));
				bf.setCreateDate(new Date());
				if(!service.add(bf)) {
					System.out.println("error");
				}
			}
			request.getRequestDispatcher(
					"/pages/student/student005/success.jsp").forward(request,
					response);
		} else if ("detail".equals(request.getParameter("status"))) {
			Student stu = (Student) new StudentServiceImpl().get(Integer.valueOf(request.getParameter("student")));
			List<BookFee> bfs = new BookFeeServiceImpl().getByObject("student", stu);
			int total = 0;
			for(int i=0;i<bfs.size();i++) {
				total += bfs.get(i).getPayFee();
			}
			request.setAttribute("total", total);
			request.setAttribute("bfs", bfs);
			request.getRequestDispatcher(
					"/pages/student/student005/fee_query_result.jsp").forward(request,
					response);
		}
	}

}
